//单选题编辑面板
Ext.define("ES.view.items.ItemSingleChoicePanel",{
    extend:"Ext.grid.Panel",
    title:"单选题选项",
    layout:{
        type:"fit",
        align:"stretch"
    },
    viewConfig:{
        forceFit:true,
        columnLines:true,
        stripRows:true
     },
    tools:[{
         type:"plus",
         tooltip:"新建选项",
         handler:function(event, toolEl, panel){
            var g = panel.up("gridpanel");
            var store = g.getStore();
            if(store){
                store.insert(0,{content:"",orderNo:store.getCount() + 1});
            }
         }
    },{
        type:"minus",
        tooltip:"删除选项",
        handler:function(event, toolEl, panel){
            Ext.MessageBox.confirm("删除提示","确认要删除选中的数据吗？", function(cfm){
                 if(cfm == "yes"){
                     var g = panel.up("gridpanel");
                     var data = g.getSelectionModel().getSelection();
                      if(data.length == 0){
                          Ext.MessageBox.alert("提示","请您选择要删除的数据!");
                      }else{
                          var store = g.getStore();
                          Ext.Array.each(data, function(record){
                            store.remove(record);
                          })
                      }
                 }
            });
        }
    }],
    store:"ItemContentStore",
    selType:"checkboxmodel",
    multiSelect:false,
    columns:[{
        text:"选项内容",
        width:198,
        flex:1,
        align:"left",
        menuDisabled:true,
        sortable:false,
        dataIndex:"content",
        editor:{
            xtype: 'textfield',
            allowBlank: false
        }
    },{
        text:"排序号",
        width:68, 
        align:"right",
        menuDisabled:true,
        sortable:false,
        dataIndex:"orderNo",
        editor:{
            xtype: 'numberfield',
            minValue:0,
            maxValue:99,
            allowBlank: false
        }
    }],
    plugins:[
        Ext.create('Ext.grid.plugin.CellEditing', {
            clicksToEdit: 1
        })
    ],
    verifyData:function(err){
        var store = this.getStore();
        if(store){
            if(store.getCount() > 0){
                var result = true;
                store.each(function(record){
                    if(record.data.content.length == 0){
                        err = "有选项内容不能为空！";
                        result = false;
                    }
                });
                if(result){
                    var data = this.getSelectionModel().getSelection();
                    if(data.length == 0){
                        err = "请选择正确的选项为标准答案！";
                        result = false;
                    }
                }
                return result;
            }
        }
        return false;
    },
    SaveItemsData:function(){
        var items = [];
        var store = this.getStore();
        store.each(function(record){
            items.push(record.data.itemID + "$" + record.data.content.replace(/\$/g,"") + "$" + record.data.orderNo); 
        });
        return items.join("$$");
    },
    SaveAnswerData:function(){
        var answer = [];
        var data = this.getSelectionModel().getSelection();
        Ext.Array.each(data,function(model){
            answer.push(model.data.itemID);
        });
        return answer.join("$$");
    },
    listeners:{
        beforerender:function(cmp){
            cmp.getStore().removeAll();
        }
    }
})